﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using ACP.Business;
using ACP.DTO;
using System.Text;
using System.Configuration;
using System.Net.Mail;


namespace ACP.Web.UI.Account
{
    public partial class Register : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            //RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];

            //string continueUrl = RegisterUser.ContinueDestinationPageUrl;
            //if (String.IsNullOrEmpty(continueUrl))
            //{
            //    continueUrl = "~/";
            //}

          
           
        }

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {
            //FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);

            if (!String.IsNullOrEmpty(RegisterUser.UserName))
            {
                Guid aspnetUserGuid = UsersLogic.GetuserGuidByUserName(RegisterUser.UserName);
                user loggedInuser = UsersLogic.GetuserByGuid(aspnetUserGuid);
                TextBox userFirstNameTextBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("FirstName");
                TextBox userLastNameTextBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("LastName");

                if (loggedInuser == null)
                {
                    loggedInuser = new user()
                    {
                        guid = aspnetUserGuid,
                        login = RegisterUser.UserName,
                        firstname = userFirstNameTextBox.Text,
                        lastname = userLastNameTextBox.Text,
                        status = true
                    };

                    UsersLogic.insertUser(loggedInuser);
                    sendEmailtoActivate();
                }

            }
            //FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);

            //Response.Redirect(continueUrl);
        }

        protected void sendEmailtoActivate()
        {
            try
            {
                string userName = RegisterUser.UserName;
                string emailAddress = RegisterUser.Email;
                TextBox userFirstNameTextBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("FirstName");
                TextBox userLastNameTextBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("LastName");
                TextBox userEmailTextBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("Email");

                // Lets get the user's id   
                Guid userId = (Guid)Membership.GetUser(userName).ProviderUserKey;
                int _mailPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SMTPPort"]);
                string _mailServer = ConfigurationManager.AppSettings["MailServer"];
                string fromAddress = ConfigurationManager.AppSettings["from"];
                string ccAddress = ConfigurationManager.AppSettings["ccAddress"];
                string toAddress = userEmailTextBox.Text.Trim();
                string smtpUserName = ConfigurationManager.AppSettings["username"];
                string smtpPassword = ConfigurationManager.AppSettings["password"];
                string redirectActiveURL = ConfigurationManager.AppSettings["redirectActivateURL"];

               
                SmtpClient smtpClient = new SmtpClient(_mailServer);

                smtpClient.Credentials = new System.Net.NetworkCredential(smtpUserName, smtpPassword);

                // Default in IIS will be localhost 
                //smtpClient.Host = "localhost";

                //Default port will be 25
                smtpClient.Port = _mailPort;
                
                MailMessage message = new MailMessage();
                message.IsBodyHtml = true;
                message.Priority = MailPriority.High;
                message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                // Now lets create an email message
                //message.Body = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'ttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
                //message.Body += "<html xmlns='http://www.w3.org/1999/xhtml'>";
                //message.Body += "<head><title>ACPTEST.com</title></head>";
                //message.Body += "<body>Dear " + userFirstNameTextBox.Text + " " + userLastNameTextBox.Text;
                //message.Body += "<br />";
                //message.Body += "Thank you for creating an account with www.acptest.com";
                //message.Body += "<br />";
                //message.Body += "Please click the below link to activate your account <br />";
                //message.Body += string.Format("<a href='" + redirectActiveURL + "?userName={0}&Id={1}'>Activate {0} </a>", userName, userId.ToString());
                //message.Body += "<br />";
                //message.Body += "Best Regards,<br />";
                //message.Body += "Team ACPTest.com";
                //message.Body += "</body></html>";


                //message.Body = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'ttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
                //message.Body += "<html xmlns='http://www.w3.org/1999/xhtml'>";
                //message.Body += "<head><title>ACPTEST.com</title> </head>";
                //message.Body += "<body style='font: normal 12px Arial, Helvetica, sans-serif;color: #000;background: #fff'>";
                //message.Body += "<div style='width:98%;padding: 10px auto;margin: 20px 40px auto;'>";
                //message.Body += "<div style='float: left;height: 113px;padding: 0px;margin: 0px;'>";
                //message.Body += "<a href=''><img src='" + redirectActiveURL + "/images/maillogo.jpg' alt='logo' /></div>";
                //message.Body += "<div style='position:relative;padding-top:200px;color:#000'>";
                //message.Body += "<b>Dear " + userFirstNameTextBox.Text + " " + userLastNameTextBox.Text + "</b>";
                //message.Body += "<br /><br />";
                //message.Body += "Thank you for creating an account with www.acptest.com.";
                //message.Body += "<br /><br />";

                //message.Body += "Please click the below link to activate your account";
                //message.Body += "<br /><br />";
                //message.Body += string.Format("<a href='" + redirectActiveURL + "/ActivateUser.aspx?userName={0}&Id={1}'>Activate {0} </a>", userName, userId.ToString());
                //message.Body += "<br /><br />";
                //message.Body += "Best Regards,<br />";
                //message.Body += "Team ACPTest";
                //message.Body += "</div></div>";
                //message.Body += "</body></html>";


                message.Body = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'ttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
                message.Body += "<html xmlns='http://www.w3.org/1999/xhtml'>";
                message.Body += "<head><title>ACPTEST.com</title> </head>";
                message.Body += "<body style='font: normal 12px Arial, Helvetica, sans-serif;color: #000;background: #fff'>";
                message.Body += "<div style='width:98%;padding: 10px auto;margin: 20px 40px auto;'>";
                message.Body += "<div style='float: left;height: 113px;padding: 0px;margin: 0px;'>";
                message.Body += "<a href=''><img src='" + redirectActiveURL + "/images/maillogo.jpg' alt='logo' /></div>";
                message.Body += "<div style='position:relative;padding-top:200px;color:#000'>";
                message.Body += "<b>Dear " + userFirstNameTextBox.Text + " " + userLastNameTextBox.Text + "</b>";
                message.Body += "<br /><br />";
                message.Body += "Thank you for creating an account with www.acptest.com.  Your account has been created successfully.";
                message.Body += "<br /><br />";

                message.Body += "Please click the below link to activate your account";
                message.Body += "<br /><br />";
                message.Body += string.Format("<a href='" + redirectActiveURL + "/Account/Login.aspx?str=Activated&user={0}'>Login {0} </a>", userName);
                message.Body += "<br /><br />";
                message.Body += "Best Regards,<br />";
                message.Body += "Team ACPTest";
                message.Body += "</div></div>";
                message.Body += "</body></html>";


                message.CC.Add(new MailAddress(ccAddress));
                message.To.Add(new MailAddress(toAddress));
                message.From = (new MailAddress(fromAddress));
                message.Subject = "www.ACPTest.com Registration Activation";

                smtpClient.Send(message);

                // Redirecto to What ever page
                //Response.Redirect("Default.aspx", true);


            }
            catch (Exception ex)
            {

                acptest_ErrorLog errorLog = new acptest_ErrorLog()
                {
                    //ex.Message, "Page_Load", "Test Page"
                    ErrorMessage = ex.Message + ex.StackTrace,
                    ErrorFunction = "NavigateQuestions()",
                    InsertedTime = DateTime.Now,
                    ModuleName = "Test.aspx"
                };
                ACPErrorLogLogic.ErrorLog(errorLog);
            }


        }

    }
}
